
import java.util.*;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

// 获取指定目录下的所有.docx文件
public class ListOfDOCX {
    public static Path pathsCurrentDir=Paths.get("").toAbsolutePath();
    // public static Path fail_out=Paths.get(pathsCurrentDir.toString(), "输出失败记录.txt");

    // public static Path directory=Paths.get(pathsCurrentDir.toString(),"fd");
    public static Path directory=pathsCurrentDir;


    public static Path fail_out=Paths.get(directory.toString(), "A输出失败记录.txt");

    // public static Path directory=pathsCurrentDir;

    public static void main(String[] args) {
        // System.loadLibrary("jacob-1.21-x64.dll");
        List<File> list_of_docx=new ArrayList<>();
        List<String> fail_lines=new ArrayList<>();
        // System.out.println(directory.toString());
        try {
            Files.walk(directory).filter(path->path.toString().toLowerCase().endsWith(".docx"))
                .forEach(path->list_of_docx.add(path.toFile()));
                    // 针对所有的File创建OneF
                List<OneF> all=new ArrayList<>();
                for (File file : list_of_docx) {
                    all.add((new OneF(file)).build());
                }
                fail_lines=new ArrayList<>();
                for (OneF oneF : all) {
                    if(oneF.isYouxiao){
                        oneF.write_to_excel();
                        // oneF.deal_txt();
                    }else{
                        fail_lines.add(oneF.toString());
                    }
                    oneF.deal_txt();
                }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // for (Path path2 : list_of_docx) {
        //     System.out.println(path2);
        // }

        try {
            fail_lines.add("该目录下所有 .docx文档已全部处理");
            FileUtils.writeLines(fail_out.toFile(), StandardCharsets.UTF_8.name(), fail_lines);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("成功");
    }
}
